package com.admin_system.config;

import org.apache.coyote.http11.AbstractHttp11Protocol;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Tomcat服务器配置类
 * 自定义Tomcat服务器配置，解决上传大文件的限制问题
 */
@Configuration
public class TomcatConfig {

    /**
     * 自定义Tomcat配置，解决"Request Entity Too Large"错误
     * 
     * @return Tomcat配置定制器
     */
    @Bean
    public WebServerFactoryCustomizer<TomcatServletWebServerFactory> tomcatCustomizer() {
        return (factory) -> {
            // 设置Tomcat上传文件大小限制
            factory.addConnectorCustomizers(connector -> {
                // 设置最大请求体大小
                connector.setMaxPostSize(325 * 1024 * 1024); // 325MB
                
                // 设置maxSwallowSize (需要转换为Http11协议)
                if (connector.getProtocolHandler() instanceof AbstractHttp11Protocol) {
                    ((AbstractHttp11Protocol<?>)connector.getProtocolHandler()).setMaxSwallowSize(-1);
                }
            });
        };
    }
} 